#!/usr/bin/perl 


use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;

use lib::Folding;

use constant {
		VERSION => 0.1
		};

my $seq_file = ''; # file with the sequence of DNA or RNA to fold
my $zones_file = '' ; #file with the zones
my $initial_temp = 35; # initial temperature
my $final_temp = 41; # final temperature
my $help_required = 0;
my $complement=0; # whether to compute the complement of the sequenece in $seq_file or not
my $must_not_package=0; # prevent to compress and remove 
my $not_run_parser_det=0; # prevent to compress and remove 
my $must_not_fold=0; # prevent to run mfold
my $folding_type=''; #Type of the folding program to use
my $resume=0;
my $daemon=0;
my $home_dir='';

&main();

sub print_help_and_exit {
    my $prog_name = shift;
    print STDERR "Usage: $prog_name --seq=file --zones=file [--itemp=temp] [--ftemp=temp] [--help]\n\n";

    print STDERR
        "\t--seq=file\tfolds the RNA or DNA sequence in `file'.\n".
        		"\t\t\tThis parameter is mandatory.\n".
        "\t--zones=file\tholds the zones to process. This file contains \n".
        		"\t\t\tlines of the form:\n".
                "\t\t\t\`zone_name, initial_nucleotide, final_nucleotide'\n".
        		"\t\t\tThis parameter is mandatory.\n".
        "\t--itemp=n\tuse `n' as initial temperature (in celsius) to compute\n".
        		"\t\t\tthe foldings.\n".
        		"\t\t\tThis parameter will be passed to the `T' parameter\n".
        		"\t\t\tof mfold. This parameter is optional\n".
        		"\t\t\tand defaults to 35.\n".
        "\t--ftemp=m\tuse `m' as final temperature. Defaults to 41 and is optional.\n".
		"\t--foldtype=type\t\tUse unafold or mfold as nucleic acid folding and hybridization prediction.\n".
        "\t--home=FullPath\t to the Home working directory\n".
        "\t--compl\t\tProcess also the complement folding.\n".
        "\t--notfold\tPrevents the script to run mfold.\n".
        "\t--notpackage\tPrevents the script to package the results.\n".
        "\t--notparse\tPrevents the script to call the det_parser.\n".
        "\t--resume\tReasume a previous folding excetution\n".
        "\t--daemon\t Execute as daemon\n".
        "\t--help\t\tprints this help and exits.\n";

    exit;
}

sub main{

    GetOptions( 'seq=s'         => \$seq_file,
                'zones=s'       => \$zones_file,
				'itemp=i'       => \$initial_temp,
				'ftemp=i'       => \$final_temp,
				'foldtype=s'    => \$folding_type,
				'compl'         => \$complement,
				'notfold'       => \$must_not_fold,
				'notpackage'    => \$must_not_package,
				'notparse'      => \$not_run_parser_det,
                'resume'        => \$resume,
                'daemon'        => \$daemon,
                'home'          => \$home_dir,
                'help'          => \$help_required);
                

    print STDERR "$0 version ".VERSION."\n";
    
	if ( ($help_required or ( $zones_file eq '' ) or ( $seq_file eq '' ) ) and (!$resume) )
    {
    #        this will be the right check when the seq file becomes optional
        print_help_and_exit($0);
    }
	
	Folding::set_folding_parameter($initial_temp,$final_temp,$seq_file,$zones_file,$complement,
		$must_not_package,$must_not_fold,$not_run_parser_det,$folding_type,$resume,$home_dir);
    Folding::run_folding($daemon);
	
}

